package com.zzb.projectfactory.controller.common;

import com.alibaba.fastjson.JSON;
import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.role.form.UserLoginForm;
import com.zzb.projectfactory.framework.role.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 公共controller 未登录可使用
 * Created by zhangzhenbin on 17-2-21.
 */
@Controller
@RequestMapping("/common")
public class CommonController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    @ResponseBody
    public BaseResponse<User> login(UserLoginForm userLoginForm, HttpServletRequest request){
        List<User> users = userService.selectAll();
        logger.info(JSON.toJSONString(users));
        BaseResponse<User> loginRet = userService.login(userLoginForm,request.getLocalAddr());
        if(loginRet.isSuccess()){
            saveUserSession(request,loginRet.getContent());
        }
        return loginRet;
    }
}
